package com.feeling.oafeeling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feeling.oafeeling.mapper.SysUserMapper;
import com.feeling.oafeeling.pojo.SysUser;
import com.feeling.oafeeling.service.ISysUserService;
import com.feeling.oafeeling.utils.util.sign.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ISysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    SysUserMapper userMapper;


    @Override
    public SysUser selectUserByUser() {
        return null;
    }

    @Override
    public SysUser findByUsername(SysUser user) {
//        //将user的password进行MD5加密
//        user.setPassword(Md5Utils.hash(user.getPassword()));

        return userMapper.findByCondition(user);
    }

    @Override
    public SysUser findById(Long id) {
        return userMapper.findFromId(id);
    }

    @Override
    public SysUser findFromId(Long id) {
        return userMapper.findFromId(id);
    }

    @Override
    public List<SysUser> findOne(SysUser user) {
        return userMapper.findOne(user);
    }

//    @Override
//    public List<SysUser> findOne(Long userId, String nickName, String deptName, Integer status) {
//        return userMapper.findOne(userId, nickName, deptName, status);
//    }

    @Override
    public boolean updatePassword(SysUser user, String password) {
        user.setPassword(Md5Utils.hash(password));
        if (userMapper.updateById(user)!=0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUser(SysUser user) {
        if (userMapper.updateUser(user)){
            return true;
        }
        return false;
    }

//    @Override
//    public IPage<SysUser> getPage(int currentPage, int pageSize, SysUser user) {
//        IPage page = new Page(currentPage,pageSize);
//        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
//
//        wrapper.select(SysUser::getUserId, SysUser::getNickName, SysUser::getSex, SysUser::getStatus);  // 指定要查询的字段
//        IPage iPage = userMapper.selectPage(page, wrapper);
//        return iPage;
//    }

    @Override
    public List<SysUser> findByPage(int currentPage, int pageSize, SysUser user) {
//        IPage page = new Page(currentPage,pageSize);
        List<SysUser> page = userMapper.findByPage(currentPage, pageSize, user);
        return page;
    }

    @Override
    public Boolean insertUser(SysUser user){
        if (user == null){
            return false;
        }
        return userMapper.insert(user) > 0;
    }

    @Override
    public Boolean updateInstitutionForUser(SysUser user, String institutionName) {
        return userMapper.updateInstitutionForUser(user.getUserId(), institutionName);
    }

    @Override
    public Boolean updatePasswordForUser(SysUser user) {
        return userMapper.updatePasswordForUser(user);
    }

    @Override
    public Boolean defaultUserNameSet(SysUser user) {
        return userMapper.defaultUserNameSet(user.getUserId(), user.getNickName());
    }

    @Override
    public Boolean updateFromId(SysUser user) {
        return userMapper.updateFromId(user);
    }

    @Override
    public String selectDeptNameById(Long deptId) {
        return userMapper.selectDeptNameById(deptId);
    }

    @Override
    public Integer getUserSize() {
        return userMapper.getUserSize();
    }


    @Override
    public SysUser findUserAndRoleById(Long id) {
        return userMapper.findUserAndRoleById(id);
    }
}
